#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2023/10/30 11:09
# @Author  : zhaowp1109
# @File    : __init__.py.py
# @Description :

from sqlalchemy import Column, Integer,DateTime,func,String
from sqlalchemy.ext.declarative import as_declarative, declared_attr
from app.utils.obj2dict import obj2dict
from snowflake import SnowflakeGenerator
def generator_snowflake_id(length=32):
    '''uuid ID'''
    gen = SnowflakeGenerator(length)
    return next(gen)
#default=generator_snowflake_id,
@as_declarative()
class Base:
    id = Column(Integer,primary_key=True, index=True)
    # id: Mapped[int] = mapped_column(primary_key=True)
    # language = Column(String(128),default="",comment="语言")
    create_at = Column(DateTime, server_default=func.now(), comment='创建时间')
    update_at = Column(DateTime, server_default=func.now(), server_onupdate=func.now(), comment='创建时间')
    is_deleted = Column(Integer, default=0, comment="删除标记")

    __name__: str
    # Generate __tablename__ automatically
    @declared_attr
    def __tablename__(cls) -> str:
        return cls.__name__.lower()
    def to_dict(self,columns=None):
        if not columns:
            columns = [key for key in self.__table__.columns.keys() if key not in ['is_deleted','update_at','language']]
        return obj2dict(self,keys=columns)